﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Drawing;

namespace jQuery.WebControls
{
    /// <summary>
    /// 
    /// </summary>
    [DefaultProperty("Title")]
    [Description("")]
    [ToolboxData("<{0}:Preloader runat=server />")]
    public class Preloader : Label
    {
        private jQueryScriptManager jQueryMngr;
        private ClientScriptProxy scriptManager;
        private Unit pBarHeight = new Unit(1, UnitType.Pixel);
        private Color pBarColor = ColorTranslator.FromHtml("#CCCCCC");

        #region PROPERTIES
        private ClientScriptProxy ClientScriptProxy
        {
            get
            {
                if (scriptManager == null)
                {
                    scriptManager = ClientScriptProxy.Current;
                }
                return scriptManager;
            }
        }
        private jQueryScriptManager jQueryManager
        {
            get
            {
                if (jQueryMngr == null)
                    jQueryMngr = Common.Instance.Get_jQueryScriptManager(Page);
                return jQueryMngr;
            }
        }
        /// <summary> 
        /// Ancho de los Icons del menu cuando el puntero no esta sobre ellos
        /// </summary>        
        [Category("Appearance")]
        [DefaultValue("1px")]
        [Localizable(false)]
        [Description("Height of the load bar")]
        public Unit BarHeight
        {
            get
            {                
                return pBarHeight;
            }
            set
            {
                pBarHeight = value;
            }
        }
        /// <summary> 
        /// Ancho de los Icons del menu cuando el puntero no esta sobre ellos
        /// </summary>        
        [Category("Appearance")]
        [DefaultValue("1px")]
        [Localizable(false)]
        [Description("Color of the load bar")]
        public Color BarColor
        {
            get
            {                
                return pBarColor;
            }
            set
            {
                pBarColor = value;
            }
        }   
        #endregion
                

        /// <summary>
        /// Check if there is a jQueryScriptManager control in the page
        /// </summary>
        /// <param name="e">Event argument</param>        
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (!Page.IsPostBack)
            {
                if (!DesignMode)
                    CheckQueryManager();
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            if (Enabled && Visible)
            {
                if (Page.Header != null)
                {
                    Page.Header.Controls.Add(GetCssClasses());
                }            
                ClientScriptProxy.RegisterStartupScript(this, this.GetType(), "PreloaderScript", 
                    "QueryLoader.init('" + Text + "');", true);
            }
        }

        private HtmlGenericControl GetCssClasses()
        {            
            //.QLoader { background-color: ; height: ;}
            //.QAmt { color:; font-family: ; font-size:; font-weight:normal; width:90%; margin:-60px 0 0 -50px;}
            if (this.BackColor.IsEmpty)
            {
                this.BackColor = ColorTranslator.FromHtml("#000000");
            }
            if (this.Font.Size.IsEmpty)
            {
                this.Font.Size = new FontUnit(20);
            }
            if (String.IsNullOrEmpty(this.Font.Name))
            {
                this.Font.Name = "Verdana";
            }
            if (this.ForeColor.IsEmpty)
            {
                this.ForeColor = ColorTranslator.FromHtml("#FFFFFF");
            }
            HtmlGenericControl style = new HtmlGenericControl("style");
            style.Attributes.Add("type", "text/css");
            style.InnerText = ".QOverlay { background-color: " + 
                ColorTranslator.ToHtml(this.BackColor).ToString() + "; z-index: 9999;}";
            style.InnerText = string.Concat(style.InnerText, ".QLoader { background-color: " + 
                ColorTranslator.ToHtml(this.BarColor).ToString() + "; height: " + this.BarHeight.Value.ToString() + "px;}");
            style.InnerText = string.Concat(style.InnerText, ".QAmt { color:" + 
                ColorTranslator.ToHtml(this.ForeColor).ToString() + 
                "; font-family: " + Font.Name.ToString() + "; ");
            style.InnerText = string.Concat(style.InnerText, "font-size:" + Font.Size.Unit.Value.ToString() + 
                "px; font-weight:" + (Font.Bold ? "bold" : "normal") + "; ");
            style.InnerText = string.Concat(style.InnerText, "width:90%; margin:-60px 0 0 -50px;}");
            return style;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            
        }

        private void CheckQueryManager()
        {
            Common.Instance.CheckJQueryScriptManager(Page);
        }
    }
}
